<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 * 获取星期几
 * @return [type] [description]
 */
function getWeek($data)
{
    $week       = date("w");
    switch ($week) {
        case '1':
            $num=0;
            break;
        case '2':
            $num=1;
            break;
        case '3':
            $num=2;
            break;
        case '4':
            $num=3;
            break;
        case '5':
            $num=4;
            break;
        case '6':
            $num=5;
            break;
        case '0':
            $num=6;
            break;
    }

    $endToday   = mktime(0,0,0,date('m'),date('d')-2,date('Y'))-1;
    return $endToday;
}
/**
 * 获取可提现余额
 * @return [type] [description]
 */
function getWeek2($user)
{
    $week       = date("w");
    switch ($week) {
        case '1':
            $num=0;
            break;
        case '2':
            $num=1;
            break;
        case '3':
            $num=2;
            break;
        case '4':
            $num=3;
            break;
        case '5':
            $num=4;
            break;
        case '6':
            $num=5;
            break;
        case '0':
            $num=6;
            break;
    }

    //这周一之前的时间
    $endToday   = mktime(0,0,0,date('m'),date('d')-$num,date('Y'))-1;

    //可提现余额
    $may=D('xllog')->where('openid="'.$user['openid'].'" and and pay_status_sk=0 status=6 and addtime<='.$endToday.' and withdraw_status=0')->getField('sum(price) as price');
    empty($may) && $may =0;

    //配置文件
    $offline = D('user')->field('tixianfeilv,offline,offline1,offline2,offline3')->find(1);
    //总收入
    $total = M('xllog')->where(array('openid'=>$user['openid'],'status'=>6))->getField('sum(price) as price');
    $total1 = M('xllog')->where(array('openid'=>$user['openid'],'status'=>6,'withdraw_status'=>0))->getField('sum(price) as price');

    //线上手续比例
    $shouxubili1 = ($offline['tixianfeilv']+$offline['offline1']+$offline['offline2']+$offline['offline3'])/100;
    //线下手续比例
    $shouxubili2 = ($offline['offline']+$offline['offline1']+$offline['offline2']+$offline['offline3'])/100;
    //线下余额 =总收入-可用余额
    $xianxia    = $total1-$user['yue'];
    
    //可提现余额-（线上服务费+线下服务费）
   if(!empty($may))
    {
        $may_money = $may-($user['yue']*$shouxubili1+$xianxia*$shouxubili2);
    }else
    {
         $may_money = 0;
    }

    $data=array(
        'keyongyue'     => $may,
        'zongshouru'    => $total,
        'xianxia'       => $xianxia,
        'ketixian'      => $may_money,
        'xianshangbili' => $shouxubili1*100
    );

    return $data;
}
function is_json($nickname){
if(is_null(json_decode($nickname))){
	return $nickname;
	
	}	else{
	return json_decode($nickname);	
		}
	
	
	}
function get_username($openid){
$user=M('wxuser')->where(array('openid'=>$openid))->getField('name');
    if (is_null(json_decode($user))) {
        $nicknames = $user;

    } else {
        $nicknames = json_decode($user);

    }
    return $nicknames;

}

function get_sijiname($openid){
$user=M('siji')->where(array('openid'=>$openid))->getField('xingming');
    if (is_null(json_decode($user))) {
        $nicknames = $user;

    } else {
        $nicknames = json_decode($user);

    }
    return $nicknames;

}
function get_usertel($openid){
    $siji = M('siji')->where(array('openid'=>$openid))->field('xingming,dianhua')->find();
    if($siji['xingming']){
        return $siji['xingming'].'<br/>'.$siji['dianhua'];
    }else{
        return $siji['dianhua'];
    }
}

/**
 * 发送邮件
 * @param  [type] $to         [发送的邮箱]
 * @param  [type] $name       [发送的名称]
 * @param  string $subject    [发送内容]
 * @param  string $body       [发送内容]
 * @param  [type] $attachment [description]
 * @return [type]             [返回是否发送成功]
 */
function think_send_mail($to, $name, $subject = '', $body = '', $attachment = null){
        $config = C('THINK_EMAIL');
        vendor('PHPMailer.class#phpmailer'); //从PHPMailer目录导class.phpmailer.php类文件
        vendor('SMTP');
        $mail = new PHPMailer(); //PHPMailer对象
        $mail->CharSet = 'UTF-8'; //设定邮件编码，默认ISO-8859-1，如果发中文此项必须设置，否则乱码
        $mail->IsSMTP(); // 设定使用SMTP服务
        $mail->SMTPDebug = 0; // 关闭SMTP调试功能
        // 1 = errors and messages

        // 2 = messages only

        $mail->SMTPAuth = true; // 启用 SMTP 验证功能

        $mail->SMTPSecure = 'ssl'; // 使用安全协议

        $mail->Host = $config['SMTP_HOST']; // SMTP 服务器

        $mail->Port = $config['SMTP_PORT']; // SMTP服务器的端口号

        $mail->Username = $config['SMTP_USER']; // SMTP服务器用户名

        $mail->Password = $config['SMTP_PASS']; // SMTP服务器密码

        $mail->SetFrom($config['FROM_EMAIL'], $config['FROM_NAME']);

        $replyEmail = $config['REPLY_EMAIL']?$config['REPLY_EMAIL']:$config['FROM_EMAIL'];

        $replyName = $config['REPLY_NAME']?$config['REPLY_NAME']:$config['FROM_NAME'];

        $mail->AddReplyTo($replyEmail, $replyName);

        $mail->Subject = $subject;

        $mail->AltBody = "为了查看该邮件，请切换到支持 HTML 的邮件客户端"; 

        $mail->MsgHTML($body);

        $mail->AddAddress($to, $name);

        if(is_array($attachment)){ // 添加附件

        foreach ($attachment as $file){

        is_file($file) && $mail->AddAttachment($file);

        }

        }

        return $mail->Send() ? true : $mail->ErrorInfo;

}

function datamsg($code,$msg,$result='')
{
    $data['code']=$code;
    $data['msg']=$msg;

    if($result)
    {
        foreach ($result as $key => $value) {
            if(empty($value))
            {
                $result[$key]='';
            }
        }
    }
    $data['result']=$result;
    echo json_encode($data);die;
}




/**
 * @param $url 接口地址 curl get请求
 * @return mixed
 */
function doGet($url)
{
    //初始化
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    // 执行后不直接打印出来
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, false);
    // 跳过证书检查
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    // 不从证书中检查SSL加密算法是否存在
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    //执行并获取HTML文档内容
    $output = curl_exec($ch);
    //释放curl句柄
    curl_close($ch);
    return $output;
}

/**
 * @param string $url
 * @param array $post_data
 * @param array | boolean $header
 * @return mixed
 */
function doPost($url,$post_data,$header=array())
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    $this_header = array(
        "content-type: application/x-www-form-urlencoded; 
        charset=UTF-8"
    );
    curl_setopt($ch,CURLOPT_HTTPHEADER,$this_header);
    // 执行后不直接打印出来
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    // 设置请求方式为post
    curl_setopt($ch, CURLOPT_POST, true);
    // post的变量
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    // 请求头，可以传数组
    curl_setopt($ch, CURLOPT_HEADER, $header);
    // 跳过证书检查
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    // 不从证书中检查SSL加密算法是否存在
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    $output = curl_exec($ch);
    curl_close($ch);
    $res=mb_convert_encoding($output, 'UTF-8', 'UTF-8,GBK,GB2312,BIG5');//使用该函数对结果进行转码
    return $res;
}


function uidname($id){
    $openid = M('wxuser')->where(array('uid'=>$id))->getField('openid');
    if($openid){
        if($openid){
            $siji = M('siji')->where(array('openid'=>$openid))->field('xingming,dianhua')->find();
            if($siji['xingming']){
                return $siji['xingming'];
            }else{
                return $siji['dianhua'];
            }
        }else{
            return '没有找到';
        }
    }else{
        return '没有找到';
    }
}